package gautobgchanger;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

/**
 * This Class is for use when you want to write new values to the config file
 * @author b1o
 */
public class ConfWriter {

    /**
     * Sets the interval of each wallpaper change
     * @param interval The interval you want to use
     * @throws Exception
     */
    public void SetInterval(String interval) throws Exception
    {
        //get the confreader object
        ConfReader cr = Main.getConfReader();
        //get the path from the config file
        String path = cr.getPath();
        FileWriter fw = new FileWriter(cr.getFile());
        PrintWriter out = new PrintWriter(fw);
        //Print the old path again to the config file
        out.println("Wallpaper_Folder = " + path);
        //Print the new value of interval to the config file
        out.println("interval = "+interval);
        //Close the streams
        out.close();
        fw.close();
    }
    /**
     * Set the path where the wallpapers are located
     * @param path The path you store the wallpapers
     * @throws Exception
     */
    public void SetPath(String path) throws Exception
    {
        ConfReader cr = Main.getConfReader();
        Integer interval = cr.getInterval();
        FileWriter fw = new FileWriter(cr.getFile());
        PrintWriter out = new PrintWriter(fw);
        out.println("Wallpaper_Folder = " + path);
        out.println("interval = "+interval);
        out.close();
        fw.close();
    }
    /**
     * Does the same as setPath(), except this one also sets the interval to
     * 60 seconds, this methode is used if there is no config file.
     * @param path The path you store your wallpapers
     * @throws Exception
     */
    public void setBasicConfig(String path) throws Exception
    {
        ConfReader cr = Main.getConfReader();
        File file = cr.getFile();
        File confPath = cr.getConfPath();
        //makes the directory for the conf file if it doesn't excist
        confPath.mkdirs();
        FileWriter fw = new FileWriter(file);
        PrintWriter out = new PrintWriter(fw);
        out.println("Wallpaper_Folder = " + path);
        out.println("interval = "+60);
        out.close();
        fw.close();
    }
}
